<?php
require_once 'phpmoko/lib/common.php';

require_once 'phpmoko/externals/TinyMCE.php';
require_once 'phpmoko/ui/Form/FormBase.php';

class RichTextArea extends FormBase
{
	private $rows = -1;
	private $cols = 80;
	private $modeSimple = false;
	private $value = null;
	private $showMenu = false;

	function __construct()
	{
		parent::__construct();

		TinyMCE::activate();
	}

	function setRows($rows)
	{
		$this->rows = $rows;
	}

	function setCols($cols)
	{
		$this->cols = $cols;
	}

	function setModeSimple($enabled)
	{
		$this->modeSimple = $enabled;
	}

	function setValue($value)
	{
		$this->value = $value;
	}

	function setShowMenu($value)
	{
		$this->showMenu = $value;
	}

	function toString()
	{
		$root = RootPanel::get();
		$js = 'tinymce.init({selector: "#' . $this->id . '",';
		$js .= 'formats: { ';
		$js .= 'bold: {inline: \'b\'},';
		$js .= 'italic: {inline: \'i\'},';
		$js .= 'underline: {inline: \'u\'},';
		$js .= '},';
		if ($this->modeSimple) {
			$js .= 'plugins: ["paste"],';
			$js .= 'valid_elements: "b/strong,p/div,p/h1,p/h2,p/h3,ul,ol,li,br,p",';
			$js .= 'toolbar: "bold bullist numlist | undo redo",';
		} else
			$js .= 'plugins: ["advlist autolink lists link image charmap print preview hr anchor pagebreak", "searchreplace wordcount visualblocks visualchars code fullscreen", "insertdatetime media nonbreaking save table contextmenu directionality", "emoticons template paste textcolor"],';

		$js .= 'image_advtab: true,' . 'menubar: ' . ($this->showMenu ? 'true' : 'false') . ',' . 'toolbar_items_size: \'small\',' . '});';

		$root->addJSCode($js);

		$str = '<textarea';

		$str .= ' id="' . $this->id . '"';

		if ($this->name != null)
			$str .= ' name="' . $this->name . '"';

		if ($this->cols != -1)
			$str .= ' cols="' . $this->cols . '"';

		if ($this->rows != -1)
			$str .= ' rows="' . $this->rows . '"';

		if ($this->getWidth() != null)
			$str .= ' style="width: ' . $this->getWidth() . '"';

		$str .= '>';

		if ($this->value != null)
			$str .= htmlspecialchars($this->value);

		$str .= '</textarea>';

		return $str;
	}
}
?>
